package com.cskaoyan.wordmemorize.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.entity.UserDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserMapper;
import com.cskaoyan.wordmemorize.dto.app.UserDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class StpInterfaceImpl implements StpInterface {

    /*
         只需要自己实现这个方法即可
     */

    @Autowired
    UserMapper userMapper;

    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        Long userId = Long.parseLong(loginId.toString());
        UserDO user = userMapper.selectById(userId);

        if (user == null || user.getLevelValue() < 2) {
            return Collections.emptyList();
        }

        // 只有等级≥2的用户才返回词书进度权限
        return levelPrivilegeMapper.selectList(
                        new LambdaQueryWrapper<LevelPrivilegeDO>()
                                .le(LevelPrivilegeDO::getLevelValue, user.getLevelValue())
                ).stream()
                .map(LevelPrivilegeDO::getPrivilegeCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return Collections.emptyList();
    }
}
